//
//  HJXTabBar.m
//  Catus
//
//  Created by 晓梦影 on 16/4/22.
//  Copyright © 2016年 黄金星. All rights reserved.
//

#import "HJXTabBar.h"

@interface HJXTabBar ()
/** 分割线*/
@property (nonatomic, weak) UIView *grayV;
/** 存放按钮数组*/
@property (nonatomic, strong) NSMutableArray *btnArray;

/** 指示器*/
@property (nonatomic, weak) UIView *redView;
@end
@implementation HJXTabBar
#pragma mark - lozy loading
- (NSMutableArray *)btnArray
{
    if (_btnArray == nil) {
        _btnArray = [NSMutableArray array];
    }
    return _btnArray;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    // 添加按钮
    for (int i = 0; i < 4; ++i) {
        UIButton *btn = [[UIButton alloc]init];
        btn.backgroundColor = [UIColor whiteColor];
        [btn setBackgroundImage:[UIImage imageNamed:@"方框4"] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setFont:[UIFont systemFontOfSize:14]];
        btn.tag = i;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
        
        // 设置按钮标题
        [self setUpBtnTitle:btn];
        [self.btnArray addObject:btn];
        [self addSubview:btn];
    }
    
    // 添加分界线
    UIView *grayV = [[UIView alloc]init];
    grayV.backgroundColor = [UIColor blackColor];
    grayV.alpha = 0.5;
    [self addSubview:grayV];
    self.grayV = grayV;
    
    // 添加指示器view
    UIView *redView = [[UIView alloc]init];
    redView.backgroundColor = [UIColor redColor];
    [self addSubview:redView];
    self.redView = redView;
    
    return self;
    
}


- (void)layoutSubviews
{
    [super layoutSubviews];

    NSInteger count = self.btnArray.count;
    CGFloat btnX = 0;
    CGFloat btnY = CGRectGetMaxY(self.grayV.frame);
    CGFloat btnW = self.width / count;
    CGFloat btnH = 35;
    for (int i = 0; i < count; ++i) {
        UIButton *btn = self.btnArray[i];
        btnX = btnW * i;
        
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
        
        // 默认选中第一个
        if (i == 0) {
            [self btnClick:btn];
        }
    }
    
    self.grayV.frame = CGRectMake(0, 0, ScreenW, 1);
    
    
}

#pragma mark - 设置按钮标题
- (void)setUpBtnTitle:(UIButton *)btn
{
    switch (btn.tag) {
        case 0:
            [btn setTitle:@"头条" forState:UIControlStateNormal];
            break;
            
        case 1:
            [btn setTitle:@"大杂烩" forState:UIControlStateNormal];
            break;
            
        case 2:
            [btn setTitle:@"贴贴" forState:UIControlStateNormal];
            break;
            
        case 3:
            [btn setTitle:@"关注" forState:UIControlStateNormal];
            break;
            
        default:
            break;
    }
}

#pragma mark - btnClick
- (void)btnClick:(UIButton *)btn
{
    // 向外界传出点击按钮的tag
    _btnTagBlock(btn.tag);
    
    // 修改指示器位置
    [UIView animateWithDuration:0.1 animations:^{
         self.redView.frame = CGRectMake(ScreenW / self.btnArray.count * btn.tag, 0, ScreenW / self.btnArray.count , 3);
    }];
   
}
@end
